{extend name="base" /}
{block name="css"}
{css href="__CSS__/login.css" /}
{/block}
{block name="body"}
<!-- 主要内容 开始 -->
<div class="weui-tab__panel">
  <div class="weui-ad">
    <img src="/static/images/logo-white.png">
    <p class="demos-sub-title f-white">{$config.sitename|default=$Think.lang.title}</p>
  </div>
  <div class="weui-cells weui-cells_form">
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">手机号</label>
      </div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="tel" id="tel" pattern="[0-9]*" maxlength="11" placeholder="请输入手机号">
      </div>
    </div>
    <div class="weui-cell weui-cell_vcode">
      <div class="weui-cell__hd">
        <label class="weui-label">验证码</label>
      </div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="number" id="code" placeholder="请输入验证码">
      </div>
      <div class="weui-cell__ft">
        <button class="weui-btn_default weui-vcode-btn">获取验证码</button>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">密&nbsp; &nbsp;码</label>
      </div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="password" id="pwd" placeholder="请输入密码">
      </div>
    </div>
    <input type="hidden" name="pid" id="pid" value="{$Request.param.code}">
  </div>
</div>
<label class="weui-agree">
  <input type="checkbox" class="weui-agree__checkbox">
  <span class="weui-agree__text">
    阅读并同意<a href="javascript:void(0);" class="open-popup" data-target="#full">《相关条款》</a>
  </span>
</label>
<div class="weui-btn-area">
  <button class="weui-btn weui-btn_primary btn">确定</button>
</div>
<div id="full" class='weui-popup__container'>
  <div class="weui-popup__overlay"></div>
  <div class="weui-popup__modal">
    <header class='demos-header'>
      <p class="demos-sub-title">《{$desc.name|default="用户注册协议"}》</p>
    </header>
    <article class="weui-article">
      {$desc.content|raw}
    </article>
    <div class="weui-btn-area">
      <button class="weui-btn weui-btn_primary close-popup">我同意</button>
    </div>
  </div>
</div>
<!-- 主要内容 结束 -->
{/block}
{block name="js"}
<script type="text/javascript">
  $(function () {
    $('.btn').click(function () {
      var agree = $('.weui-agree__checkbox').prop('checked');
      var tel = $('#tel').val();
      var pwd = $.trim($('#pwd').val());
      var code = $('#code').val();
      var pid = $('#pid').val();
      if(!agree) return $('#full').popup();
      else if(!tel || !/^1[3456789]\d{9}$/.test(tel)) return $.toptip('请输入有效的手机号码!');
      else if(!code || !/^\d{6}$/.test(code)) return $.toptip('请输入六位手机验证码!');
      else if(!pwd) return $.toptip('密码不能为空哟!');
      else if(pwd.length<6) return $.toptip('请输入六位以上密码哟!');
      else $.post('/register.html',{mobile:tel,pwd:pwd,code:code,pid:pid},function(res) {
        if (res.code === 1) {
          $.toast(res.msg,'text');
          setTimeout(function(){
            if (is_weixin()) {
              window.close();
              WeixinJSBridge.call('closeWindow');
            }else{
              location.href = res.url;
            }
          },1000);
        } else {
          $.toptip(res.msg);
        }
      });
    });
    $('.close-popup').click(function(){
      $('.weui-agree__checkbox').attr('checked',true);
    });
    var count = 90; //间隔函数，1秒执行
    var InterValObj1; //timer变量，控制时间
    var curCount1;//当前剩余秒数
      $('.weui-vcode-btn').click(function(){
        curCount1 = count;
        var tel = $.trim($('#tel').val());
        if (!/^1[3456789]\d{9}$/.test(tel)) return $.toptip('请输入有效的手机号码!');
        else $.post('/captcha.html',{phone:tel,type:''},function(result){
          if (!result.code) return $.toptip(result.msg);
          //设置button效果，开始计时
          $(".weui-vcode-btn").attr("disabled", "true");
          $(".weui-vcode-btn").html(+curCount1 + "秒再获取");
          $.toptip(result.msg);
          InterValObj1 = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
         //向后台发送处理数据
        });
     });

    function SetRemainTime() {
      if (curCount1 == 0) {
          window.clearInterval(InterValObj1); //停止计时器
          $(".weui-vcode-btn").removeAttr("disabled"); //启用按钮
          $(".weui-vcode-btn").html("重新发送");
      } else {
          curCount1--;
          $(".weui-vcode-btn").html(+curCount1 + "秒再获取");
      }
    }
  });
</script>
{/block}